package com.wzcl.app.dao.repository;

import com.wzcl.app.dao.model.UserTaskQuizAnswer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.beans.Transient;
import java.util.List;

public interface UserTaskQuizAnswerRepository extends JpaRepository<UserTaskQuizAnswer, Integer> {

    //查询用户回答了的题(包括语音题和非语音题)
    @Query(value = "select utq from UserTaskQuizAnswer utq where utq.taskId = ?1 and utq.phone = ?2")
    List<UserTaskQuizAnswer> getQuestionByAnswer(Integer taskId, String phone);

    //根据任务id查询用户已经回答过的题id
    @Query(value = "select utq.questionBankId from UserTaskQuizAnswer utq where utq.taskId = ?1 and utq.phone = ?2")
    List<Integer> getAnswerQuestionByTaskId(Integer taskId, String phone);

    //根据任务id查询用户答错的题
    @Query(value = "select utq.questionBankId from UserTaskQuizAnswer utq where utq.taskId = ?1 and utq.answerResult = 0 and utq.phone = ?2")
    List<Integer> getMistakeQuestionByTaskId(Integer taskId, String phone);

    //通过题库的题目id检索用户是否答题
    @Query(value = "select utq from UserTaskQuizAnswer utq where utq.questionBankId = ?1 and utq.taskId = ?2 and utq.phone = ?3")
    UserTaskQuizAnswer findIdByQuestionIdAndTaskId(Integer questionId, Integer taskId, String phone);

    //根据任务id和用户手机删除答题记录
    @Transient
    @Modifying
    @Query(value = "delete from UserTaskQuizAnswer utq where utq.taskId = ?1 and utq.phone = ?2")
    void deleteByTaskId(Integer taskId, String phone);
}